package com.org.zp.ctrl.myzhaopin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.ctrl.search.SearchJobActivity;
import com.org.zp.entity.login.Resume;

public class ZpHomeActivity extends BaseActivity {

	private TextView headerTitle; // 顶部条标题
	private Button back_btn, top_right_btn;

	private RelativeLayout hr_letter_layout;// 人事经理来信
	private RelativeLayout job_apply_layout;// 职位申请记录
	private RelativeLayout job_favorite_layout;// 职位收藏夹
	private RelativeLayout search_subscription_layout;// 搜索与订阅

	private RecApp mApp;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zp_home);

		mApp = (RecApp) getApplication();

		if (!(ActivityTaskManager.getInstance().containsName("ZpHomeActivity"))) {
			ActivityTaskManager.getInstance().putActivity("ZpHomeActivity",
					this);
		}

		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("个人中心");

		back_btn = (Button) findViewById(R.id.back);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"ZpHomeActivity");
			}
		});

		top_right_btn = (Button) findViewById(R.id.top_right_button);
		top_right_btn.setVisibility(View.GONE);
		top_right_btn.setVisibility(View.GONE);

		progressDialog = ProgressDialog.show(ZpHomeActivity.this, "提示",
				"数据加载中,请稍后...", true, true);

		setUpViews();

		new getResumeInfo().execute(URLHelper.mPrefix);
	}

	private void setUpViews() {
		// 人事经理来信
		hr_letter_layout = (RelativeLayout) findViewById(R.id.hr_letter_layout);
		hr_letter_layout.setOnClickListener(listener);

		// 职位申请记录
		job_apply_layout = (RelativeLayout) findViewById(R.id.job_apply_layout);
		job_apply_layout.setOnClickListener(listener);

		// 职位收藏夹
		job_favorite_layout = (RelativeLayout) findViewById(R.id.job_favorite_layout);
		job_favorite_layout.setOnClickListener(listener);

		// 搜索与订阅
		search_subscription_layout = (RelativeLayout) findViewById(R.id.search_subscription_layout);
		search_subscription_layout.setOnClickListener(listener);
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent;
			switch (v.getId()) {
			case R.id.hr_letter_layout:
				intent = new Intent(ZpHomeActivity.this, HrletterActivity.class);
				startActivity(intent);
				break;
			case R.id.job_apply_layout:
				intent = new Intent(ZpHomeActivity.this,
						ApplyRecordActivity.class);
				startActivity(intent);
				break;
			case R.id.job_favorite_layout:
				intent = new Intent(ZpHomeActivity.this,
						FavoriteRecordActivity.class);
				startActivity(intent);
				break;
			case R.id.search_subscription_layout:
				intent = new Intent(ZpHomeActivity.this,
						SearchJobActivity.class);
				intent.putExtra("full", true);
				startActivity(intent);
				break;
			}

		}
	};

	class getResumeInfo extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "myResumeList");
			map.put("uid", mApp.user.getuID());

			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(ZpHomeActivity.this, "失败",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(ZpHomeActivity.this, "网络超时",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else {
				try {
					String succeed = new JSONObject().getString("succeed");
					if (succeed.equals("000")) {

						String data = new JSONObject().getString("dataList");
						JSONArray list = new JSONArray(data);

						ArrayList<Resume> rem = new ArrayList<Resume>();

						for (int i = 0; i < list.length(); i++) {
							Resume info = new Resume();
							JSONObject log = list.getJSONObject(i);
							info.setResume_id(log.getString("id"));
							info.setResume_title(log.getString("title"));
							info.setIsdefaultflag(log.getString("audit_cn"));
							info.setResume_talent_cn(log.getString("talent_cn"));
							info.setShow_count(log.getString("click"));

							rem.add(info);

						}

						mApp.user.setResume(rem);

					}
				} catch (Exception e) {
					e.printStackTrace();
				}

				init();

			}

		}
	}

	private void showToast(String msg) {
		Toast toast = Toast.makeText(ZpHomeActivity.this, msg,
				Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	private void init() {
		ArrayList<Resume> resumelist = mApp.user.getResume();
		if (null == resumelist || resumelist.size() < 1) {
			// apple
			Intent intent = new Intent(ZpHomeActivity.this,
					CreateResumeActivity.class);
			startActivity(intent);
			ActivityTaskManager.getInstance().removeActivity("ZpHomeActivity");
		} else {
			// 简历视图
			View resume_layout = findViewById(R.id.resume_layout);
			resume_layout.setVisibility(View.VISIBLE);
		}
		// 初始化简历管理
		ResumeMgr resumeMgr = new ResumeMgr(ZpHomeActivity.this);
		resumeMgr.setResumeMgrListener(new ResumeMgr.ResumeMgrListener() {

			@Override
			public void onComplete() {
				progressDialog.dismiss();
			}
		});
		resumeMgr.show();
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = null;
			// switch (v.getId()) {
			// case R.id.hr_letter_layout:// 转向到人事经理来信
			// intent = new Intent(ZpHomeActivity.this, HrLetterActivity.class);
			// startActivity(intent);
			// break;
			// case R.id.job_apply_layout:// 职位申请记录
			// intent = new Intent(ZpHomeActivity.this,
			// PositionApplicationActivity.class);
			// startActivity(intent);
			// break;
			// case R.id.job_favorite_layout:// 转向到职位收藏夹
			// intent = new Intent(ZpHomeActivity.this,
			// JobFavoriteActivity.class);
			//
			// startActivity(intent);
			// break;
			// case R.id.search_subscription_layout:// 搜索与订阅
			// intent = new Intent(ZpHomeActivity.this,
			// SSPositionActivity.class);
			// startActivity(intent);
			// break;
			// }
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity("ZpHomeActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
